package es.us.lsi.daw.servicios;



import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MostrarFichero {

	
	public void imprimirPdf(ByteArrayOutputStream out,HttpServletResponse response,HttpServletRequest request) throws IOException{
		response.setContentType("application/pdf");
		response.addHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
		//response.addHeader("Pragma", "no-cache");
		response.setDateHeader ("Expires",0);
		//response.addHeader("Cache-Control","no-store"); 
		String navegador = request.getHeader("User-Agent");
		boolean esFirefox = (navegador != null && navegador.indexOf("Firefox/") != -1);
		if(esFirefox)
			response.addHeader("Cache-Control","no-store");		
		response.setContentLength(out.size()); 
		ServletOutputStream output = response.getOutputStream(); 
		out.writeTo(output); 
		output.flush(); 
	}
	
	public void imprimirPdf(ByteArrayOutputStream out,HttpServletResponse response,HttpServletRequest request,String nombre) throws IOException{
		response.setContentType("application/pdf");
		response.addHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
		//response.addHeader("Pragma", "no-cache");
		response.setDateHeader ("Expires",0);
		//response.addHeader("Cache-Control","no-store"); 
		String navegador = request.getHeader("User-Agent");
		boolean esFirefox = (navegador != null && navegador.indexOf("Firefox/") != -1);
		if(esFirefox)
			response.addHeader("Cache-Control","no-store");	
		
		response.setHeader("Content-Disposition", "inline;filename=\""+nombre+".pdf\"");
		response.setContentLength(out.size()); 
		ServletOutputStream output = response.getOutputStream(); 
		out.writeTo(output); 
		output.flush(); 
	}
	
	
	public void imprimir(ByteArrayOutputStream out,String tipo,Object response) throws IOException{
		OutputStream output = null;
		if(response instanceof HttpServletResponse){			
			HttpServletResponse responseAux = (HttpServletResponse)response; 
			responseAux.setContentType(tipo);
			responseAux.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
			responseAux.setHeader("Pragma", "no-cache");				
			responseAux.setContentLength(out.size()); 
			output = responseAux.getOutputStream();
		}
		if(output != null){			 
			out.writeTo(output); 
			output.flush();
		}
	}
	
	public void imprimir(ByteArrayOutputStream out,String tipo,HttpServletResponse response,String nombre) throws IOException{
		response.setContentType(tipo);
		response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		String nombreC="";
		String []aux=nombre.split(" ");
		for(int i=0;i<aux.length;i++)
			nombreC+=aux[i];
		response.setHeader("Content-Disposition", "attachment;filename="+nombreC);
		response.setContentLength(out.size()); 
		ServletOutputStream output = response.getOutputStream(); 
		out.writeTo(output); 
		output.flush(); 
	}
	
	public void imprimir(Blob fichero,String tipo,Object response){
		try{
			if(fichero != null){
				ByteArrayOutputStream out = new ByteArrayOutputStream();
		        InputStream in = fichero.getBinaryStream();
		        int length = (int) fichero.length();
		
		        int bufferSize = 1024;
		        byte[] buffer = new byte[bufferSize];
		
		        while ((length = in.read(buffer)) != -1) {
		          out.write(buffer, 0, length);
		        }
		        in.close();
		        imprimir(out,tipo,response);
				out.flush();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String tipoMIMEArchivo (String nombreArchivo){
		String extension = nombreArchivo.substring(nombreArchivo.lastIndexOf('.')+1);
		if(extension.equalsIgnoreCase("pdf"))
			return "application/pdf";
		else if(extension.equalsIgnoreCase("zip"))
			return "application/zip";
		else if(extension.equalsIgnoreCase("doc"))
			return "application/msword";
		else if(extension.equalsIgnoreCase("xls"))
			return "application/vnd.ms-excel";
		else if(extension.equalsIgnoreCase("ppt"))
			return "application/vnd.ms-powerpoint";
		else if(extension.equalsIgnoreCase("docx"))
			return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
		else if(extension.equalsIgnoreCase("xlsx"))
			return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
		else if(extension.equalsIgnoreCase("pptx"))
			return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
		else if(extension.equalsIgnoreCase("gif"))
			return "image/gif";
		else if(extension.equalsIgnoreCase("png"))
			return "image/png";
		else if(extension.equalsIgnoreCase("jpe"))
			return "image/jpg";
		else if(extension.equalsIgnoreCase("jpg"))	
			return "image/jpg";
		return "application/force-download";
	}
	
}
